package cn.acewill.yxd.yunxundian.ui.schedule;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.componet.NetWorkStateReceiver;
import cn.acewill.yxd.yunxundian.model.Schedule;
import cn.acewill.yxd.yunxundian.pubClass.pubClass;
import cn.acewill.yxd.yunxundian.ui.MainActivity;

import static cn.acewill.yxd.yunxundian.pubClass.pubClass.Server_namehzm;

public class zidingyiAdd extends AppCompatActivity implements View.OnClickListener {

    public Context mContext;
    protected static Schedule tempSch=new Schedule();
    protected int empid = 0;
    protected int schid = 0;
    private EditText tvShowDialog,shijian,neirong;
    private Calendar cal;
    private int year,month,day;
    private String sYear,sMonth,sDay,sHour,sMin;
    private Snackbar snackbar;
    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zidingyi_add);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mContext=this;
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
        toolbar.setTitle("");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getDate();

        tvShowDialog=(EditText) findViewById(R.id.dateText);
        tvShowDialog.setOnClickListener(this);
        shijian = (EditText)findViewById(R.id.timeText);
        shijian.setOnClickListener(this);
        neirong = (EditText)findViewById(R.id.titleStr);
        empid = MainActivity.id;
        snackbar = Snackbar.make(findViewById(R.id.zdyContent), "正在与服务器交互，请稍候。。。", Snackbar.LENGTH_LONG).setAction("Action", null);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(neirong.getText().equals(""))
                {
                    snackbar.setText("请输入备注内容");
                    snackbar.setDuration(1500);
                    return;
                }
                snackbar.setText("正在与服务器交互，请稍候。。。");
                Gson gson=new Gson();
                Map<String,Object> map=new HashMap<>();
                map.put("task","addZidingyiNew");
                map.put("empid",empid+"");
                map.put("titleStr",neirong.getText());
                map.put("dateStr",tvShowDialog.getText()+" "+shijian.getText());
                if (NetWorkStateReceiver.whetherConnectNetWork==1){
                    snackbar.show();
                    pubClass.Post(Server_namehzm,map,new pubClass.MyCallBack<String>(){
                        @Override
                        public void onSuccess(String result) {
                            super.onSuccess(result);
                            String[] strings=result.split("\\|");
                            if(strings[0].equals("200")) {
                                Gson gson=new Gson();
                                Type typeList=new TypeToken<ArrayList<Schedule>>(){}.getType();
                                ArrayList<Schedule> schedules=gson.fromJson(pubClass.convertToArrayJson(result),typeList);
                                try {
                                    db.saveOrUpdate(schedules);
                                    tvShowDialog.setText(schedules.get(0).getSchStartTime().split("T")[0]);
                                    neirong.setText("");
                                } catch (DbException e) {
                                    e.printStackTrace();
                                }
                                snackbar.setText("提交自定义任务成功");
                                snackbar.setDuration(1500);
                            }
                        }
                        @Override
                        public void onError(Throwable ex, boolean isOnCallback) {
                            super.onError(ex, isOnCallback);
                            snackbar.setText("推送失败请稍后再试");
                            snackbar.setDuration(1500);
                        }
                    });
                }
            }
        });
    }

    //获取当前日期
    private void getDate() {
        cal=Calendar.getInstance();
        year=cal.get(Calendar.YEAR);       //获取年月日时分秒
        Log.i("nyr","year"+year);
        month=cal.get(Calendar.MONTH);   //获取到的月份是从0开始计数
        day=cal.get(Calendar.DAY_OF_MONTH);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dateText:
                DatePickerDialog.OnDateSetListener listener=new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker arg0, int year, int month, int day) {
                        sYear = year+"";
                        if(month+1<10)
                        {
                            sMonth = "0"+(month+1);
                        }
                        else
                        {
                            sMonth = ""+(month+1);
                        }
                        if(day<10)
                        {
                            sDay = "0"+day;
                        }
                        else{
                            sDay = ""+day;
                        }
                        tvShowDialog.setText(sYear+"-"+sMonth+"-"+sDay);      //将选择的日期显示到TextView中,因为之前获取month直接使用，所以不需要+1，这个地方需要显示，所以+1
                    }
                };
                DatePickerDialog dialog=new DatePickerDialog(mContext, 0,listener,year,month,day);//后边三个参数为显示dialog时默认的日期，月份从0开始，0-11对应1-12个月
                dialog.show();
                break;
            case R.id.timeText:
                TimePickerDialog.OnTimeSetListener listener1 = new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        sMin=minute+"";
                        sHour = hourOfDay+"";
                        if(sHour.length()==1)
                        {
                            sHour = "0"+sHour;
                        }
                        if(sMin.length()==1)
                        {
                            sMin = "0"+sMin;
                        }
                        shijian.setText(sHour+":"+sMin);
                    }
                };
                TimePickerDialog dialog1 = new TimePickerDialog(mContext,0,listener1,12,0,true);
                dialog1.show();
                break;
            default:
                break;
        }
    }

}
